package UI_EJB;


import javax.ejb.*;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.annotation.Resource;
import java.util.*;


/**
 * Created by IntelliJ IDEA.
 * User: mchebanenko
 * Date: 17-Aug-2010
 * Time: 12:17:09 PM
 * To change this template use File | Settings | File Templates.
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class UserInfoEntityBean implements IUserInfo {

    public UserInfoEntityBean(){}

    @PersistenceContext(unitName="Hostel_persistence")
    private EntityManager entityManager;

    @Resource
    private SessionContext context;


    public List getAllUsersInfo (){
        Query qr = entityManager.createQuery("select t from UserInfoEntity t") ;
        return qr.getResultList() ;
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public int SaveUsersInfo (UserInfoEntity ui){
        try{

            UserInfoEntity uie = new UserInfoEntity();
            uie.setDoc_ID(ui.getDoc_ID());
            uie.setUser_First_Name(ui.getUser_First_Name() ) ;
            uie.setUser_Last_Name(ui.getUser_Last_Name());
            uie.setCreate_Date(ui.getCreate_Date());
            uie.setDoc_Amonut(ui.getDoc_Amonut());
            uie.setCredit_Amount(ui.getCredit_Amount());
            uie.setCredit_Long(ui.getCredit_Long());
            uie.setRate(ui.getRate());
            entityManager.joinTransaction() ;
            entityManager.merge(uie);

            return 1;
        }  catch(Exception e) {
            System.err.println("Exception: " + e.getMessage());
            context.setRollbackOnly();
            return 0;
        }


    }



}
